//
//  MoreAnchorViewController.swift
//  FM
//
//  Created by x on 2017/11/6.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class MoreAnchorViewController: UIViewController {
    
    var index:Int = 0
    

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        anchorTableView.frame = self.view.bounds
    }
    
    func setupUI() -> Void {
        
        view.backgroundColor = UIColor.white
        navigationItem.leftBarButtonItem = UIBarButtonItem.creatBarButtonItem(imageName: "back", target: self, action: #selector(leftBackBtuClick))
        view.addSubview(anchorTableView)
        
        //加载数据
        loadAnchor(offset: 0)
    }
    
    func leftBackBtuClick() -> Void {
        navigationController?.popViewController(animated: true)
    }
    
    /// 加载更多主播数据
    func laodMoreAnchor() -> Void {
        index += 1
        let num = index * 20
        loadAnchor(offset: num)
    }
    
    /// 加载主播数据
    ///
    /// - Parameter offset: 数据的偏移量
    func loadAnchor(offset:Int) -> Void {
        NetWorTools.shareNetWorkTools().loadMoreAnchor(offset: offset) { (data, error) in
            if error == nil {
                self.anchorList += data!
                self.anchorTableView.reloadData()
            }else {
                print("loadMoreAnchor=======",error!)
            }
        }
    }
    
    //MARK:懒加载
    private lazy var anchorTableView:UITableView = {
        let tableView = UITableView()
        tableView.dataSource = self
        tableView.delegate = self
        
        tableView.mj_footer = MJRefreshBackNormalFooter(refreshingTarget: self, refreshingAction: #selector(laodMoreAnchor))

        tableView.register(HotRadioTableViewCell.self, forCellReuseIdentifier: hotAnchorCellIdentifier)
        
        return tableView
    }()
    
    fileprivate lazy var anchorList:[Radio] = [Radio]()
    
    fileprivate lazy var cachesHeight:[String:CGFloat] = [String:CGFloat]()
    
}

private let hotAnchorCellIdentifier = "hotAnchorCellIdentifier"

extension MoreAnchorViewController:UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return anchorList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: hotAnchorCellIdentifier) as! HotRadioTableViewCell
        
        cell.radio = anchorList[indexPath.row]
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        
        let radioDetailsVC = RadioDetailsViewController()
        radioDetailsVC.currentRadio = anchorList[indexPath.row]
        navigationController?.pushViewController(radioDetailsVC, animated: true)
        
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        let model = anchorList[indexPath.row]
        
        let height = cachesHeight[model.id!]
        
        guard height == nil else {
            return height!
        }
        
        let cell = tableView.dequeueReusableCell(withIdentifier: hotAnchorCellIdentifier) as! HotRadioTableViewCell
        
        let rowHeight = cell.rowHeight(model: model)
        
        cachesHeight[model.id!] = rowHeight
        
        return rowHeight
    }
}
